package com.ubhave.powertrackernof1;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.util.Log;

public class PageSensorLogger{
	
	SharedPreferences settings;
	SharedPreferences.Editor editor;
	
	LifeGuideConnector lgc;
	String usr;
	String intervention;
	
	LocationManager lm;
	ConnectivityManager cm;
	NetworkInfo ni;
	
	WifiManager wifiMgr;
	WifiInfo wifiInfo;
	
	Context context;
	
	PowerTrackerGPSLocationListener gpsl;
	PowerTrackerNetworkLocationListener netl;
		
	public PageSensorLogger(Context c){
		
		context = c;
		
		settings=PreferenceManager.getDefaultSharedPreferences(PowerTrackerApplication.getContext());
		editor = settings.edit();
		
		lgc=new LifeGuideConnector(settings);    	
        usr = settings.getString("LifeGuideUser", "");
		intervention = settings.getString("LifeGuideInterventionId", "");
		
		//Location
		lm = (LocationManager)c.getSystemService(Context.LOCATION_SERVICE);
		gpsl = new PowerTrackerGPSLocationListener();
		netl = new PowerTrackerNetworkLocationListener();
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000L, 0, netl);
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000L, 0, gpsl);
			
		//Connectivity
		cm = (ConnectivityManager)c.getSystemService(Context.CONNECTIVITY_SERVICE);
		ni = cm.getActiveNetworkInfo();
		
		
		
		//Wifi
		
			wifiMgr = (WifiManager)c.getSystemService(Context.WIFI_SERVICE);
			wifiInfo = wifiMgr.getConnectionInfo();
		
	}
		
	
	
	public void makeLog() throws PowerTrackerInvalidAuthKeyException{
		String appStartSensors = "Page("+context.getClass().getSimpleName()+"): ";
		
		if(!settings.getString("CurrentLat", "").equals("")&&!settings.getString("CurrentLng", "").equals("")&&!settings.getString("CurrentLocSensor", "").equals("")){
			Log.i("POWeR Tracker Sensor: ","BASIC LocSense: "+settings.getString("CurrentLat", "")+" "+settings.getString("CurrentLng", ""));
			appStartSensors+="LocationSensor("+settings.getString("CurrentLocSensor", "")+") Latitude("+settings.getString("CurrentLat", "")+") Longitude("+settings.getString("CurrentLng", "")+") ";
		}
		
		String networkType;
		if(ni==null)
			networkType="None";
		else
			networkType=ni.getTypeName();
		
		Log.i("POWeR Tracker Sensor: ","BASIC NetSense: "+networkType);
		appStartSensors+="Connectivity("+networkType;
		
		if(networkType.equals("WIFI")){
			Log.i("POWeR Tracker Sensor: ","BASIC WifiSense: "+wifiInfo.getSSID());
			appStartSensors+="-"+wifiInfo.getSSID()+") ";
		}
		else
			appStartSensors+=") ";
		
		//Commit and post current Log
		String prevLog = settings.getString("sensorLog", "");
		editor.putString("sensorLog", settings.getString("sensorLog", "")+appStartSensors+" TimeStamp("+new Date()+"), ");
        editor.commit();
        if(!usr.equals("")&&!intervention.equals("")){
        	String serverLog = lgc.getVar(intervention, usr, "user", "appSensorLog",settings.getString("authkey",""));
        	if(serverLog!=null&&!serverLog.equals("")&&(prevLog.length()<serverLog.length()||(prevLog.length()>serverLog.length()&&!prevLog.substring(0, serverLog.length()).equals(serverLog)))){
				editor.putString("sensorLog",serverLog);		
		        editor.commit();
		        makeLog();
			}
        	else
        		lgc.postVar(intervention, usr, "user", "appSensorLog", settings.getString("sensorLog", ""),settings.getString("authkey",""));
        }
        else
        	Log.i("POWeR Tracker: ", "Log fail usr: "+usr+" int: "+intervention);
	}
	
	public void stopLocationSensing(){
		gpsl = new PowerTrackerGPSLocationListener();
		netl = new PowerTrackerNetworkLocationListener();
		lm.removeUpdates(gpsl);
		lm.removeUpdates(netl);
	}
	
	public void resumeLocationSensing(){
		gpsl = new PowerTrackerGPSLocationListener();
		netl = new PowerTrackerNetworkLocationListener();
		//Log.i("POWeR Tracker Special: ", lm+" "+gpsl+" "+netl);
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000L, 0, netl);
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000L, 0, gpsl);		
	}
	
}